package lumis.doui.control.itempicker;

import org.w3c.dom.Node;

import lumis.doui.control.ControlContainer;
import lumis.doui.control.IControl;
import lumis.doui.control.tabulardata.TabularDataControl;
import lumis.doui.source.ISourceData;
import lumis.doui.source.TabularSource;
import lumis.doui.source.field.ITabularSourceField;
import lumis.portal.PortalException;
import lumis.util.TextUtil;
import lumis.util.XmlUtil;

/**
 * Used to render data from a tabular data source with item selection functionality.
 * <br/>
 * Once the user selects an item, a provided callback function is called in 
 * the window opener before closing the current window.
 * <br/> 
 * Example use:
 * <xmp>
 * <control:lum_selectItemTabularData sourceId="voteOption"/>
 * </xmp>
 * 
 * xml data available for xsl rendering:
 * <xmp>
 * <control id="8A488A0311570496011157BFD84114F0" sourceId="voteOption" type="lum_selectItemTabularData">
 * 	<!-- same as lum_tabularData control -->
 * 	<data>
 * 		<!-- same as lum_tabularData control -->
 * 		<callbackFunction>callbackFunctionName</callbackFunction>
 * 	</data>
 * </control>
 * </xmp>
 * 
 * @see TabularDataControl
 * @version $Revision: 8618 $ $Date: 2007-12-18 11:34:20 -0200 (Tue, 18 Dec 2007) $
 * @since 4.1.0
 */
public class SelectItemTabularDataControl extends TabularDataControl
{
	private String callbackFunction;
	private String callbackFunctionRequestParameterName;
	private String primaryKeyFieldId;
	private String primaryNameFieldId;

	@Override
	public void init(Node controlDefinitionNode, ControlContainer controlContainer, IControl parentControl) throws PortalException
	{
		super.init(controlDefinitionNode, controlContainer, parentControl);
		
		this.callbackFunctionRequestParameterName = XmlUtil.readNodeString("callbackFunctionRequestParameterName", controlDefinitionNode, "callbackFunction");
		this.primaryKeyFieldId = XmlUtil.readNodeString("primaryKeyFieldId", controlDefinitionNode);
		this.primaryNameFieldId = XmlUtil.readNodeString("primaryNameFieldId", controlDefinitionNode);
		
		if(primaryKeyFieldId == null || primaryNameFieldId == null)
		{
			for(ITabularSourceField field : getSource().getFields())
			{
				if(primaryKeyFieldId == null && field.isPrimaryKey())
					primaryKeyFieldId = field.getId();
				if(primaryNameFieldId == null && field.isPrimaryName())
					primaryNameFieldId = field.getId();
			}
		}
	}

	public void loadFromRequest() throws PortalException
	{
		super.loadFromRequest();
		
		callbackFunction = douiContext.getRequest().getParameter(getId()+"_callbackFunction");
		
		if(callbackFunction == null && !douiContext.isPostback() && callbackFunctionRequestParameterName != null)
		{
			callbackFunction = douiContext.getRequest().getParameter(getCallbackFunctionRequestParameterName());
		}
	}
	
	public void setRenderData() throws PortalException
	{
		super.setRenderData();

		if(callbackFunction != null)
		{
			Node dataNode = XmlUtil.selectSingleNode("data", this.controlDefinitionNode);
			XmlUtil.addTextNode(dataNode, "callbackFunction", callbackFunction);
		}
	}
	
	@Override
	protected String getRowHyperlink(TabularSource source, ISourceData row, String compositePrimaryKey, int rowIndex) throws PortalException
	{
		String primaryKey = row.get(getPrimaryKeyFieldId()).toString();
		String primaryName = row.get(getPrimaryNameFieldId()).toString();
		
		if(callbackFunction != null)
			return "javascript:window.opener."+callbackFunction+"(\""+TextUtil.escapeJs(primaryKey)+"\",\""+TextUtil.escapeJs(primaryName)+"\");window.close();";
		else
			return super.getRowHyperlink(source, row, compositePrimaryKey, rowIndex);
	}

	public String getCallbackFunction()
	{
		return callbackFunction;
	}

	public void setCallbackFunction(String callbackFunction)
	{
		this.callbackFunction = callbackFunction;
	}

	public String getCallbackFunctionRequestParameterName()
	{
		return callbackFunctionRequestParameterName;
	}

	public void setCallbackFunctionRequestParameterName(String callbackFunctionRequestParameterName)
	{
		this.callbackFunctionRequestParameterName = callbackFunctionRequestParameterName;
	}

	public String getPrimaryKeyFieldId()
	{
		return primaryKeyFieldId;
	}

	public void setPrimaryKeyFieldId(String primaryKeyFieldId)
	{
		this.primaryKeyFieldId = primaryKeyFieldId;
	}

	public String getPrimaryNameFieldId()
	{
		return primaryNameFieldId;
	}

	public void setPrimaryNameFieldId(String primaryNameFieldId)
	{
		this.primaryNameFieldId = primaryNameFieldId;
	}
}
